package com.liguo.whoisthespy.utils

import club.ddkx.duoduokaixin.util.DelegatesExt
import club.ddkx.duoduokaixin.util.ke
import com.liguo.whoisthespy.bean.UserBean
import com.liguo.whoisthespy.bean.WordsTbl
import io.realm.Realm
import liguo.application.BaseApplication
import liguo.views.BaseActivity
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.doFromSdk
import org.jetbrains.anko.doIfSdk
import org.jetbrains.anko.uiThread
import java.io.BufferedReader
import java.io.InputStreamReader

/**
 * Created by su on 2017/6/15.
 */

/**
 * 保存词语到数据库
 */
fun BaseActivity.readWodi(){
    val t = this
    if(realm.where(WordsTbl::class.java).count()>0) return
    doAsync{
        ke("开始了")
        val is_ = BaseApplication.getContext().classLoader.getResourceAsStream("assets/wodi.txt")
        val br = BufferedReader(InputStreamReader(is_, Charsets.UTF_8))
        val list = br.readLines().mapIndexed{ index, s ->
            with(WordsTbl()){
                val vs = s.split(" ")
                id = index
                civilian = vs[0]
                undercover = vs[1]
                this
            }
        }
        Config.wordsNumber = list.size
        ke(Config.wordsNumber.toString())
        uiThread {
            t.listSave(list)
        }
        is_.close()
    }

}

object Config{
//    var dbVerson by DelegatesExt.preference(BaseApplication.getContext(),"dbVerson","0")
    var wordsNumber:Int by DelegatesExt.preference(BaseApplication.getContext(),"wordsNumber",0)
    var isDisableHistory:Boolean by DelegatesExt.preference(BaseApplication.getContext(),"isDisableHistory",false)//是否禁用历史词
}